package net.sourceforge.jweb.jstl.easyui;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import net.sourceforge.jweb.annotation.Attribute;
import net.sourceforge.jweb.annotation.Tag;
import net.sourceforge.jweb.json.JSONException;
import net.sourceforge.jweb.json.JSONObject;
import net.sourceforge.jweb.jstl.JsSupport;
import net.sourceforge.jweb.jstl.helper.JstlUtil;

/**
 * column tag
 */
@Tag(name = "column", description = "datagrid column tag")
public class DataGridColumnTag extends BaseEasyuiTag implements JsSupport {
	private final static Logger LOGGER = LoggerFactory.getLogger(DataGridColumnTag.class);
	private static final long serialVersionUID = 1L;

	// exclude this column from column display list
	private boolean exclude = false;
	private String field;
	private String title;
	private Integer width;
	private String align;
	private boolean sortable = true;
	private boolean editable = true;
	private boolean filterable = true;
	private String order;
	private boolean checkbox;
	private String overrideEditorType;
	private int columnIndex = -1;

	public DataGridColumnTag() {
		super();
		LOGGER.debug("new DataGridColumnTag instance");
	}

	@Override
	public int doStartTag() throws JspException {
		properties = new LinkedHashMap<String, Object>();
		return super.doStartTag();
	}

	@Override
	public int doEndTag() throws JspException {
		LOGGER.debug("render grid column");
		if (!StringUtils.hasText(field)) {
			throw new JspException("field must have value");
		}

		DataGridTag parentTag = (DataGridTag) this.getParent();
		Map<String, Object> columnAttrMap = parentTag.columnConfigurations.get(field);
		if (columnAttrMap == null) {
			throw new JspException("field " + field + " not found in grid clumns");
		}

		// this column filter disabled
		if (!filterable) {
			parentTag.columnFilters.remove(field);
		}
		if (!editable) {
			columnAttrMap.remove("editor");
		}
		if (exclude) {
			parentTag.columnConfigurations.remove(field);
			parentTag.columnFilters.remove(field);
		} else {

			try {
				this.properties.putAll(JstlUtil.tagAttibuteMap(this));
			} catch (Exception e) {
				e.printStackTrace();
				throw new JspException(e);
			}
			// 排序列,用户没有设置则不加入properties，父grid自动计算了列
			if (columnIndex == -1) {
				this.properties.remove("columnIndex");
			}

			if (StringUtils.hasLength(overrideEditorType)) {
				columnAttrMap.put("editor", JstlUtil.defaultDataGridEditor(overrideEditorType.trim().toLowerCase()));
				parentTag.columnFilters.put(field, JstlUtil.defaultColumnFilter(field,
						overrideEditorType.trim().toLowerCase(), parentTag.getGridId()));
			}
			if (this.entity != null) {
				try {
					mergeJsonObject(this.properties, (JSONObject) this.entity.getWrappedObject());
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			mergeMap(this.properties, columnAttrMap);
		}

		return EVAL_PAGE;
	}

	public void setExclude(boolean exclude) {
		this.exclude = exclude;
	}

	public void setField(String field) {
		this.field = field;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public void setSortable(boolean sortable) {
		this.sortable = sortable;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	public boolean isExclude() {
		return exclude;
	}

	@Attribute(required = true, description = "field name", quote = "'")
	public String getField() {
		return field;
	}

	public String getTitle() {
		return title;
	}

	public Integer getWidth() {
		return width;
	}

	public String getAlign() {
		return align;
	}

	public boolean isSortable() {
		return sortable;
	}

	public String getOrder() {
		return order;
	}

	public boolean isCheckbox() {
		return checkbox;
	}

	public int getColumnIndex() {
		return columnIndex;
	}

	public void setColumnIndex(int columnIndex) {
		this.columnIndex = columnIndex;
	}

	@Attribute(required = false, description = "set column editable or not")
	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public boolean isFilterable() {
		return filterable;
	}

	public void setFilterable(boolean filterable) {
		this.filterable = filterable;
	}

	@Attribute(required = false, description = "lower case of java type name, it is used to generate editor UI")
	public String getOverrideEditorType() {
		return overrideEditorType;
	}

	public void setOverrideEditorType(String overrideEditorType) {
		this.overrideEditorType = overrideEditorType;
	}
}
